package com.ftdigital.primemovers_operators.fragment;


import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.MyApplication;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.adpter.OilReportListViewAdapter;
import com.ftdigital.primemovers_operators.adpter.OilReportReleaseListViewAdapter;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.model.BaseModel;
import com.ftdigital.primemovers_operators.model.DayInventory;
import com.ftdigital.primemovers_operators.model.Entity;
import com.ftdigital.primemovers_operators.model.OilReprotEntity;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;
import com.google.gson.JsonSyntaxException;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.io.Serializable;

/**
 * 油量报表
 */
@EFragment(R.layout.fragment_oil_report)
public class OilReportFragmentRelease extends CoreFragment<OilReprotEntity> {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.listview)
    RecyclerView listView;

    @ViewById(R.id.last_month_total)
    TextView lastMonthTotal;

    OilReportReleaseListViewAdapter oilReportReleaseListViewAdapter;
    public String getCachePrifix() {
        return "DayInventory";
    }



    @AfterViews
    public void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_oil_reprot));
        listView.setLayoutManager(new LinearLayoutManager(getActivity()));

        loadData();
    }

    @Override
    protected Object readList(Serializable serializable) {
        return serializable;
    }

    @Override
    public void requestData() {
        requestParams = "gasstation_id=" + MyApplication.userInfo.getData().getGasstation_id();
        requestUrl = RequestAddr.oilReprotList + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }

            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                //设置最新更新时间
                MyApplication.pushRefreashTime(getCache(), System.currentTimeMillis());
                executeParserTask(object);
            }
        });
    }

    @Override
    protected OilReprotEntity parseList(Object object) {
        try {
            oilReprotEntity = gson.fromJson(object.toString(), OilReprotEntity.class);
            return oilReprotEntity;
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return null;
        }
    }

    OilReprotEntity oilReprotEntity;

    /**
     *是否有错误信息
     *设置Errorlayout的状态， 如为空，无网络等
     *如果为首页则清理adapter数据
     *检查获取数据中与现有数据重复数据 删除
     *如果获取的数据size 为零
     *如果size小于页面大小
     *添加数据
     * @param oilReprotEntity
     */
    @Override
    protected void executeOnLoadData(OilReprotEntity oilReprotEntity) {
        oilReportReleaseListViewAdapter = new OilReportReleaseListViewAdapter(getActivity().getApplicationContext(), oilReprotEntity);
        listView.setAdapter(oilReportReleaseListViewAdapter);
        listView.setLayoutParams(new LinearLayout.LayoutParams(listView.getLayoutParams().width, listView.getLayoutParams().height * oilReportReleaseListViewAdapter.getItemCount()));
        if (oilReprotEntity.getData().getLast_months().equals("")) {
            lastMonthTotal.setText("0");
        } else {
            lastMonthTotal.setText(oilReprotEntity.getData().getDays());
        }
    }


    @Click(R.id.back_btn)
    @Override
    public void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
        }
    }


}
